<?php

// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2030 http://www.jmzh1688.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: lxl
// | jmzhfsauto 2020-11-11 14:11:18
// +----------------------------------------------------------------------


namespace app\admin\controller;

use think\Controller;
use think\Db;

use app\admin\model\AuthorizationModel;


class Authorization extends Base
{



    public function getWhereFromPara()
    {
        $param = input('param.');
        $where=[];

        
        if(array_key_exists('',$param)) {$where['']=$param[''];}  
        

        return $where;
    }


    
    public function _initialize()
    {
        $wherep=$this->getWhereFromPara();
        $furl=url('authorization/index' ,$wherep);
        $faddurl=url('authorization/add' ,$wherep);
        $fediturl=url('authorization/edit' ,$wherep);
        
        $this->assign([
            'nowurl' => $furl,
            'addurl' => $faddurl,
            'editurl'=> $fediturl
            
        ]);
       
    }
 

    public function index()
    {

        $wherep=$this->getWhereFromPara();
    

        if(request()->isAjax()){
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            $authorizationmodel = new AuthorizationModel();


            $where = getWhereForm( $param);
            $where['isdelete']=0;
            $whereor=[];

            
            


            $where=array_merge($where,$wherep);
    

            $list = $authorizationmodel->getByWhere($where, $offset, $limit, $whereor);
            foreach($list as $key=>$vo){
                $list[$key]['operate'] = $this->getRowButton($vo,$wherep);
            }
          
            

            $result['total'] =$authorizationmodel->getAllCount($where, $whereor);  // 总数据
            $result['rows'] = $list;
            return json($result);
        }
 

        $toolbarshow= $this->getToolBar($wherep);

        $this->assign([
            'toolbar' => $toolbarshow
        ]);
       

        return $this->fetch();

    }

    public function del()
    {
        $authorizationmodel = new AuthorizationModel();
        if(request()->isPost()){
            $id = input("post.id");
            $msg = $authorizationmodel->delById($id);
            return json(msg($msg['code'], $msg['data'], $msg['msg']));
        }
        return $this->fetch();
    }

    public function edit()
    {
        $authorizationmodel = new AuthorizationModel();

        if(request()->isPost()){
            $param = input("post.");

            
 
            foreach ($param as $key=>$value){
                if(is_array($value))
                {
                    $param[$key]=implode(",",$param[$key]);
                }
            }
           
            $wherep=$this->getWhereFromPara();
            $parastr="";
            foreach ($wherep as $key => $value)
            {
                $parastr=$parastr.$key."=".$value."&";
            }


            

            $msg = $authorizationmodel->edit($param);
            return json(msg($msg['code'], $msg['data']."?". $parastr, $msg['msg']));
        }
      
        $id = input('param.id');
        $info=$authorizationmodel->getById($id);
  
        $this->assign([
            'info' => $info
        ]);
        return $this->fetch();
    }

    public function add()
    {
    

        if(request()->isPost()){
            $authorizationmodel = new AuthorizationModel();
            $param = input("post.");
            $wherep=$this->getWhereFromPara();
            $param=array_merge($param,$wherep);

           
            $parastr="";
            foreach ($wherep as $key => $value)
            {
                $parastr=$parastr.$key."=".$value."&";
            }

			
			
			
			foreach ($param as $key=>$value){
                if(is_array($value))
                {
                    $param[$key]=implode(",",$param[$key]);
                }
            }
			
			
			
            
            

            

            $msg = $authorizationmodel->add($param);
            return json(msg($msg['code'], $msg['data']."?". $parastr, $msg['msg']));
        }
    
        return $this->fetch();
    }



  
        private function getRowButton($vo,$wherep)
        {
           
            $id=$vo['id'];
            
            $idwhere=$wherep;

            $idwhere['id']=$id;

     
         
            $operatestr='[{"uuid":"edit","title":"编辑","href":"'.url('edit', ['id' => $id]).'","icon":"","style":"","para":"","isblank":"0","isnouse":0},{"uuid":"del","title":"删除","href":"javascript:del(this,$id)","icon":"","style":"","para":"","isblank":"0","isnouse":0},{"uuid":"guidbc3bfa","title":"生成word","href":"'.url('phpword', ['id' => $id]).'","icon":"","style":"","para":"","isblank":"0","isnouse":0}]';

            $arr=$vo->toArray();
            foreach ($arr as $key => $value)
            {
                $operatestr=str_replace("$".$key,$value, $operatestr);
            }

               $operate=json_decode($operatestr,true);
              $option= getButtonHtml($operate);
                return $option;
    
        }


        private function getToolBar($where)
        {
           
          
            $operatestr='[{"uuid":"add","title":"添加","href":"'.url('add' ,$where).'","icon":"","style":"btn btn-outline-primary btn-sm ","para":"","isblank":"0","isnouse":0}]';
               foreach ($where as $key => $value)
               {
                   $operatestr=str_replace("$".$key,$value, $operatestr);
               }
             

               $operate=json_decode($operatestr,true);
              $option= getButtonHtml($operate);
                return $option;
    
        }
        
        /**/
    public function phpword(){
        $id = input('param.id');
        $res =(new AuthorizationModel)->get($id)->toArray();

        $phpWord = new \PhpOffice\PhpWord\PhpWord();
        $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('static/admin/template/authorized.docx');

        $templateProcessor->setValue('authorized_company',$res['authorized_company']);
        $templateProcessor->setValue('company_number',$res['company_number']);
        $templateProcessor->setValue('by_authorized_company',$res['by_authorized_company']);
        $templateProcessor->setValue('brand',$res['brand']);
        $templateProcessor->setValue('start_time',date('Y年m月d日',strtotime($res['start_time'])));
        $templateProcessor->setValue('end_time',date('Y年m月d日',strtotime($res['end_time'])));

        $path = $templateProcessor->save(ROOT_PATH .'/static/admin/template/entrynotice.docx');
        // 下载文件到浏览器
        if (!file_exists($path)){
            echo  "没有该文件";
            return;
        }else{
            ob_clean();
            ob_start();
            $filename = $res['by_authorized_company'] .'授权书.doc';
            $fp = fopen($path, "r");
            $file_size = filesize($path);
            Header("Content-type:  application/octet-stream ");
            Header("Accept-Ranges:  bytes ");
            Header("Accept-Length:".$file_size);
            Header("Content-Disposition:  attachment;  filename= $filename");
            $buffer=1024;
            $file_count=0;
            $file  = fopen($path, "r");
            while (!feof($file) && $file_count<$file_size) {
                $file_con=fread($fp,$buffer);
                $file_count +=$buffer;
                echo $file_con;
            }
            fclose($fp);
            ob_end_flush();
        }
    }
    
}
